package com.easytouch.core.encryption;

import java.io.UnsupportedEncodingException;

/**
 * 用来进行ET加解密的类
 * Created by gulin on 2017/2/22.
 */

public class ETEncrypt {

    /**
     * 计算url参数签名
     * @param pOrder 订单号22位
     * @param pOrigin 要加密的url参数串
     * @return 签名
     */
    public static String sign(final String pOrder, final String pPrice, final String pOrigin)
    {
        if(pOrder == null || pPrice == null || pOrigin == null)
            return null;
        if(pOrder.length() < 20)
            return null;
        //取订单号后20位
        String newOrder = pOrder.substring(pOrder.length() - 20);
        //每5位一组，分4组
        String[] group = new String[4];
        for (int i = 0; i < group.length; i++) {
            int p = i * 5;
            group[i] = newOrder.substring(p, 5 + p);
        }
        //奇数组对调，偶数组对调
        String temp = group[0];
        group[0] = group[2];
        group[2] = temp;
        temp = group[1];
        group[1] = group[3];
        group[3] = temp;
        //拼接在一起
        newOrder = "";
        for (String g: group) {
            newOrder += g;
        }
        //计算随机数
        //取订单时间中的日、时、分、秒
        String time = pOrder.substring(pOrder.length() - 8);
        int number;
        try
        {
            number = Integer.parseInt(time);
            number = (int)(number / (Double.parseDouble(pPrice) * 100.0));
        }
        catch (NumberFormatException e)
        {
            return null;
        }
        //和之前的串拼接在一起
        newOrder += String.valueOf(number);
        //拼接到要计算签名的串后面
        newOrder = pOrigin + newOrder;
        //MD5加密
        return MD5.md5Encode(newOrder);
    }

    /**
     * 对串进行加密
     * @param pOrigin 要加密的串
     * @return 加密后的串
     */
    public static String encode(final String pOrigin)
    {
        if(pOrigin == null)
            return null;
        if(pOrigin.length() < 6)
            return null;
        //先对要加密的串进行base64加密
        String result;
        try {
            result = Base64.encode(pOrigin.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            return null;
        }
        result = swapBit(result);
        return result;
    }

    /**
     * 对串进行解密
     * @param pEncoded 要解密的串
     * @return 解密后的串
     */
    public static String decode(final String pEncoded)
    {
        if(pEncoded == null)
            return null;
        if(pEncoded.length() < 1)
            return null;
        String result;
        result = pEncoded;
        result = swapBit(result);
        result = new String(Base64.decode(result));
        return result;
    }

    private static String swapBit(final String pStr)
    {
        String result = pStr;
        //倒数第一位与正数第5位调换
        String part = result.substring(4, 5); //第5位
        String temp = result.substring(0, 4);
        temp += result.substring(result.length() - 1);
        temp += result.substring(5, result.length() - 1);
        temp += part;
        result = temp;
        //倒数第二位与正数第2位调换
        part = result.substring(1, 2); //第二位
        temp = result.substring(0, 1);
        temp += result.substring(result.length() - 2, result.length() - 1);
        temp += result.substring(2, result.length() - 2);
        temp += part;
        temp += result.substring(result.length() - 1);
        result = temp;
        return result;
    }
}
